/**
 * 产品分类列表
 * 文件命名格式：zmn.模块名称.表名称.功能.jas
 * @author duanzuocai
 * @since 2018/11/01
 */
layui.define(['index', 'form', 'table', 'laypage', 'layedit', 'laydate'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var table = layui.table;
    var laypage = layui.laypage;

    var cols = [
        [
            {field: 'relateId', title: 'ID', fixed:'left', width: 175, align: 'center', unresize: true, sort: false},
            {field: 'manageCompany',fixed:'left', width: 175, title: '公司', align: 'center', unresize: true, sort: false},
            {field: 'province', title: '省', width: 175, align: 'center', unresize: true, sort: false},
            {field: 'city', title: '市', width: 175, align: 'center', unresize: true, sort: false},
            {field: 'county', title: '区县', width: 175, align: 'center', unresize: true, sort: false},
            {
                field: 'mapData', title: '地图数据', width: 175, align: 'center', unresize: true, templet: function (d) {
                    if (zmnUtil.isEmptyNull(d.mapData)) {
                        return "全部";
                    }
                    return d.mapData;
                }
            },
            {field: 'updater', title: '编辑人', width: 175, align: 'center', unresize: true,
                templet: function (row) {
                    if (row.updater) {
                        return row.updater;
                    } else {
                        return "--";
                    }

                }
            },
            {
                field: 'updateTime', title: '编辑时间', templet: '#createTime', width: 175, align: 'center', unresize: true,
                templet: function (row) {
                    if (row.updateTime) {
                        return zmnUtil.createTime(row.updateTime);
                    } else {
                        return "--";
                    }

                }
            },
            {field: 'creater', title: '创建人', width: 175, align: 'center', unresize: true},
            {
                field: 'createTime', title: '创建时间', templet: '#createTime', width: 175, align: 'center', unresize: true,
                templet: function (row) {
                    return zmnUtil.createTime(row.createTime);
                }
            },
            {fixed: 'right', title: '操作', width: 270, align: 'center', templet:
                '<div>' +
                (json.hasServ ? '<button class="layui-btn layui-btn-sm" onclick="serviceManager({{d.relateId}});"lay-event="add">开通分类</button>' : '') +
                (json.hasMap ? '<button class="layui-btn layui-btn-sm" onclick="drawMap({{d.relateId}});"lay-event="add">画地图</button>' : '') +
                (json.hasDel ? '<button class="layui-btn layui-btn-danger layui-btn-sm" onclick="return delArea({{d.relateId}});"lay-event="add">删除</button>' : '') +
                '</div>'
        }
        ]
    ];
    table.render({
        elem: "#zmn-companyarea-table",
        title: "分类列表",
        data: json.items,
        cols: cols,
        limit: json.pageSize,
        page: true,
        height: 'full-120'
    });

    laypage.render(zmnPage.laypageOptions(json));

    // 区域下拉框联动
    zmnPage.laySelectLinkage(form, "/common/area/listChilds.action", "provinceId", "cityId", null);
    zmnPage.laySelectLinkage(form, "/common/company/listManageCompanyByBizCompanyId.action", "companyId", "manageCompanyId", null);

    if (json.vo.cityId != null) {
        let formSelects = layui.formSelects;
        initSelect(formSelects, json.vo.cityId);
        layui.formSelects.render();
    }

    form.on("select(cityId)", function (obj) {
        if (zmnUtil.isNull(obj) || obj.value == 0) {
            return;
        }
        let formSelects = layui.formSelects;
        initSelect(formSelects, obj.value);
        layui.formSelects.render();
    });

    //对外暴露的接口
    exports('art_companyarea_list', {});
});

function initSelect(formSelects, parentId) {

    formSelects.config('countyId', {
        keyName: 'value',
        keyVal: 'key',
        keyChildren: 'list',
        success: function (id, url, val, result) {
        },
        error: function (id, url, val, err) {
            console.log(id + ":" + err);
        }
    });

    /**
     * 1.多选数据赋值
     *
     * formSelects.data(ID, type, config);
     *
     * @param ID            xm-select的值
     * @param type          'local' | 'server', 本地数据或者远程数据
     * @param config        配置项
     *          arr             本地数据数组
     *          url             远程数据链接
     *          keyword         远程数据搜索内容, 其他附加数据可以使用 formSelects.config设置
     *          linkage         是否为联动多选（就是把上级地扯关联一起显示/选择，true时必须全择到最底层一条线才行）
     *          linkageWidth    联动多选没级宽度
     */
    formSelects.data('countyId', 'server', {
        url: '/common/area/listVtDtoChilds.action?parentId=' + parentId,
        linkage: false,
        linkageWidth: 130
    });

    formSelects.render();
}

// 添加
function addArea() {
    try {
        let manageCompanyId = layui.$("#manageCompanyId").val();
        let companyId = layui.$("#companyId").val();
        let provinceId = layui.$("#provinceId").val();
        let cityId = layui.$("#cityId").val();
        let countyArr = layui.formSelects.value('countyId');
        let countyIds = '';
        for (let i = 0; i < countyArr.length; i++) {
            countyIds += i == countyArr.length - 1 ? countyArr[i].key : countyArr[i].key + ',';
        }

        if (manageCompanyId == 0 || provinceId == 0 || cityId == 0 || countyIds == '') {
            layer.alert("公司、省、市、区县必须选择！");
            return false;
        }
        zmnPage.ajaxSimple("/company/area/add.action",
            {"manageCompanyId": manageCompanyId, "provinceId": provinceId, "cityId": cityId, "countyIds": countyIds, "companyId": companyId}, 1500);
        return false;

    } catch (e) {
        alert("添加异常：" + e);
    }
    return false;
}

function serviceManager(relateId) {
    layer.open({
        type: 2,
        title: '开通分类',
        area: ['90%', '80%'],
        content: '/company/area/serviceManager.action?relateId=' + relateId
    });
}

function drawMap(relateId) {
    layer.open({
        type: 2,
        title: '画地图',
        area: ['90%', '80%'],
        content: '/company/area/drawMap.action?relateId=' + relateId
    });
}

// 删除
function delArea(relateId) {
    layer.confirm("确定要删除吗?", {
        btn: ['确定', '取消']
    }, function () {
        try {
            if (relateId == null || relateId == 0) {
                layer.alert("参数relateId异常：" + relateId);
                return false;
            }
            zmnPage.ajaxSimple("/company/area/del.action", {"relateId": relateId}, 1500);
        } catch (e) {
            alert("添加异常：" + e);
        }
        return false;
    })
}

function reload(msg) {
    parent.layui.layer.msg(msg);
    window.location.reload();
}
